package com.itheima;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*
Stream流：
    方便对流水线上的数据进行过滤、筛选，去重。。。操作

    1、获取stream流
        单列集合
            list.stream()
            set.stream()
        双列集合
            间接生成
            keySet/entry-->set-->set.stream()
        数组
            Arrays.stream(arr)
        同类型的多个数据（可变参数）
            Stream.of(数据1，数据1，数据1，。。。)
    2、中间操作--返回还是流
            filter()：过滤
            limit()：只取前几个
            skip()：跳过前几个
            distinc():去重
            concat():合并两个流
    3、终结操作--不再返回stream流，只能操作一次,只能重新获取流
            forEach():
            count():
    收集数据：
        Stream的
 */
public class Demo_Stream {
    public static void main(String[] args) {
        //1、获取stream流
        /*//单列集合
        ArrayList<String> list = new ArrayList<>();
            list.add("aaa");
            list.add("bbb");
            list.add("ccc");
            list.stream().forEach(s-> System.out.println(s));
        //双列集合
        HashMap<String,Integer> hm = new HashMap<>();
        hm.put("zhangsan",23);
        hm.put("lisi",24);
        hm.put("wangwu",25);
        hm.put("zhaoliu",26);
        hm.put("tianqi",27);
        hm.keySet().stream().forEach(s -> System.out.println(s));
        hm.entrySet().stream().forEach(s-> System.out.println(s));
        //数组
        int[] arr = {1,2,3,4,5};
        Arrays.stream(arr).forEach(s-> System.out.println(s));
        //可变参数
        Stream.of(1,2,3,4,5).forEach(s-> System.out.println(s));*/
        //2、中间操作--返回还是该值
        /*//list.stream().filter():过滤操作
        //"张三丰","张无忌","张翠山","王二麻子","张良","谢广坤"
        ArrayList<String> list = new ArrayList<>(
                List.of("张三丰", "张无忌", "张无忌", "张翠山", "王二麻子", "张良", "谢广坤"));
        list
                .stream()//获取流
                .filter(s -> s.startsWith("张")&&s.length() == 3)//第一次过滤：以张开头的
//              .filter(s -> s.length() == 3)//第二次过滤 长度为3的
                .forEach(s -> System.out.println(s));//遍历*/
        /*//list.stream().limit(long s):截取前几个指定参数个数的数据
        list.stream().limit(2).forEach(s-> System.out.println(s));
        //list.stream().skip(long s):跳过指定参数个数的数据
        list.stream().skip(2).forEach(s-> System.out.println(s));
        //list.stream().distinct():去除重复元素
        list.stream().distinct().forEach(s-> System.out.println(s));*/
        //Stream.concat(stream1,stream2):合并两个流
        /*ArrayList<String> list1 = new ArrayList<>(
                List.of("张三丰", "张无忌", "张无忌", "张翠山", "王二麻子", "张良", "谢广坤"));
        ArrayList<String> list2 = new ArrayList<>(
                List.of("关羽", "张飞", "赵子龙", "马超", "黄忠"));
        Stream.concat(list1.stream().filter(s->s.length()==2),list2.stream().filter(s->s.length()==3)).forEach(s-> System.out.println(s));*/
        //终结操作：forEach() count()
        Integer[] arr = {10,-15,20,20,20,22,-26,-26,30,35};
        Arrays.stream(arr)
                .filter(s->s>=0&&s<=100)
                .filter(s->s%2==0)
                .distinct()
                .forEach(s-> System.out.println(s));
        /*long count = Arrays.stream(arr)
                .filter(s -> s >= 0 && s <= 100)
                .filter(s -> s % 2 == 0)
                .distinct()
                .count();
        System.out.println(count);*/
        //collection----toList/toSet
        /*ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            list.add(i);
        }
        list.add(10);
        list.add(10);
        list.add(10);
        List<Integer> list1 = list.stream()
                .filter(s -> s % 2 == 0)
                .collect(Collectors.toList());
        System.out.println(list1);
        System.out.println("-------------------------------");
        Set<Integer> set = list.stream()
                .filter(s -> s % 2 == 0)
                .collect(Collectors.toSet());
        System.out.println(set);*/
        //toMap
       /* ArrayList<String> list = new ArrayList<>(List.of("张三,23","李四,24","王五,25"));
        System.out.println(list.stream()
                .filter(s -> Integer.parseInt(s.split(",")[1]) >= 24)
                .collect(Collectors.toMap(s -> s.split(",")[0]
                        , s -> s.split(",")[1])));
        System.out.println(list.stream()
                .filter(s -> *//*Integer.parseInt(s.split(",")[1]) >= 24*//*{
                    String[] split = s.split(",");
                    int i = Integer.parseInt(split[1]);
                    return i >= 24;
                })
                .collect(Collectors.toMap(s -> s.split(",")[0]
                        , s -> s.split(",")[1])));*/
        //
        /*ArrayList<String> manActor = new ArrayList<>();
        manActor.add("周润发");
        manActor.add("成龙");
        manActor.add("刘德华");
        manActor.add("吴京");
        manActor.add("周星驰");
        manActor.add("李连杰");
        ArrayList<String> womanActor = new ArrayList<>();
        womanActor.add("林心如");
        womanActor.add("张曼玉");
        womanActor.add("林青霞");
        womanActor.add("柳岩");
        womanActor.add("林志玲");
        womanActor.add("王祖贤");
        Stream<String> stream1 = manActor.stream().filter(name -> name.length() == 3).limit(3);
        Stream<String> stream2 = womanActor.stream().filter(name -> name.startsWith("林")).skip(1);
        *//*Stream.concat(stream1,stream2).forEach(name-> System.out.println(new Actor(name)));*//*
        //System.out.println(Stream.concat(stream1, stream2).map(s -> new Actor(s)).collect(Collectors.toList()));
        System.out.println(Stream.concat(stream1, stream2).map(Actor::new).collect(Collectors.toList()));*/
    }
}
/*class Actor  {
    private String name;

    public Actor() {
    }

    public Actor(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Actor{" +
                "name='" + name + '\'' +
                '}';
    }
}*/
